package commonclass;

import org.junit.Test;

import java.util.Arrays;

/**
 * @author lwmzzz
 * @version 1.0
 * @date 2021/3/21 17:56
 * @description java 常用类 String
 */
public class StringTest {
    @Test
    public void test1() {
        String a = "abc";//字面量
        String s = new String("abc");
        System.out.println(a == s);
        String b = "abc";
        System.out.println(a == b);//true
        a = "aaa";
        System.out.println(a);//aaa
        System.out.println(b);//abc

        System.out.println("******************************");
        String s1 = "a";
        s1 += "aac";
        System.out.println("******************************");
        String s2 = "aaaa";
        String s3 = s2.replace("a", "v");
        System.out.println(s2);
        System.out.println(s3);
    }

    @Test
    public void test2() {
        //字面量
        String s = "ABC";
        String s1 = "ABC";

        String s2 = new String("ABC");
        String s3 = new String("ABC");
    }

    @Test
    public void test3() {
        String s1 = "a";
        String s2 = "b";
        String s3 = "ab";
        String s4 = "a" + "b";
        System.out.println(s1 + s2 == s3);
        System.out.println(s4 == s3);
        //intern方法：返回String 会去常量池寻找，存在则返回对应的引用，反之加入常量池返回
        String s5 = (s1 + s2).intern();
        System.out.println(s5 == s4);
    }

    @Test
    public void test4() {
        String s = "adsadsahdjkashdj";
        //return value.length;
        System.out.println(s.length());
        //return value[index];
        System.out.println(s.charAt(3));
        // return value.length == 0;
        System.out.println(s.isEmpty());
        System.out.println(s.toUpperCase());

        System.out.println(s.toLowerCase());
        //去掉空白 ：''
        System.out.println(s.trim());
        //引用值相等？内容相等
        System.out.println(s.equals("1"));
        //同上但是忽略大小写
        System.out.println(s.equalsIgnoreCase("1"));
        //return 一个新的String
        System.out.println(s.concat("132312"));
        //挨个比
        System.out.println(s.compareTo("1"));
        //返回新的String 截取
        System.out.println(s.substring(2));
    }

    @Test
    public void test5() {
        String s = "fhsdofdhsklf";
        System.out.println(s.endsWith("s"));
        System.out.println(s.startsWith("f"));
        System.out.println(s.contains("f"));

        //未找到返回-1
        System.out.println(s.indexOf("a"));
        System.out.println(s.lastIndexOf("a"));
    }

    @Test
    public void test6() {
        String s = "abcdefg";
        System.out.println(s.replace("ab", "Hello World"));
        System.out.println(s.matches("a"));
        String s1 = "ds1hj1hj32khj44gjhkj23l1k3h3h43h43k4h32j";

        //todo java 正则
        System.out.println(s1.replaceAll("\\d+", ",").replaceAll("^,|,$", ""));
        String s2 = s1.replaceAll("\\d+", ",").replaceAll("^,|,$", "");
        String[] split = s2.split(",");
        for (String s3 : split) {
            System.out.println(s3);
        }
    }

    @Test
    /**
     * String和其他结构的转换
     */
    public void test7() {
        System.out.println(Integer.parseInt("31"));
        {
            try {
                System.out.println(Integer.parseInt("31L"));

            } catch (Exception e) {
                System.out.println(e);
            }
        }
        System.out.println(Double.parseDouble("21D"));
        System.out.println(Float.parseFloat("21F"));
        System.out.println(Long.parseLong("21"));
        String s = String.valueOf(3213213);
        System.out.println(s);
        System.out.println(Integer.toString(3123));
    }

    @Test
    public void test8() {

        String s = "313213gdgd";
        char[] chars = s.toCharArray();
    }

    @Test
    public void test9() {
        String s = "eqweqweqwe";
        byte[] bytes = s.getBytes();
        for (byte byte1 : bytes) {
            System.out.println(byte1);
        }
        String s1 = "%……%%……&*&￥%￥%";
        byte[] s1Bytes = s1.getBytes();
        System.out.println(Arrays.toString(s1Bytes));
    }

    @Test
    public void test10() {
        StringBuffer stringBuffer = new StringBuffer("qwer");
        stringBuffer.length();
        stringBuffer.setCharAt(0, 'a');
        StringBuffer fddfsf = stringBuffer.append("fddfsf");
        StringBuilder stringBuilder = new StringBuilder();
        int length = stringBuilder.length();
        System.out.println(new StringBuffer("qwer").indexOf("qwer"));
    }

    @Test
    public void test11() {
        String s = null;
        StringBuffer stringBuffer = new StringBuffer();
        System.out.println(stringBuffer.append(s));

        System.out.println(new StringBuffer(s));
    }
}
